<template>
  <div id="login-modal" v-show="visible">
    <div class="modal-wrap">
      <div class="login-wrap">
        <span class="iconfont login-close" @click="visible =false">&#xe608;</span>
        <p class="login-title">登录或注册ivva</p>
        <button type="primary" class="login-btn login-wechat" :disabled="!checked" hover-class="none"
                open-type="getPhoneNumber" @getphonenumber="_getPhoneNumber">
          <img class="iv-icon-wx" src="https://www.ivvajob.com/img/wxMin/common/wx.png">
          <span class="iv-ml10">微信帐号快速登录</span>
        </button>
        <!--<button class="login-btn login-tel" :disabled="!checked" hover-class="none" bindtap="_loginTel">-->
        <!--<img class="iv-icon-tel" src="/static/images/common/phone.png">-->
        <!--<span class="iv-ml10">手机号注册/登录</span>-->
        <!--</button>-->
        <view class="tips">
          <label class="check-label" @click="checked=!checked">
            <checkbox :checked="checked" color="#ff4f64" class="checkbox"/>
            <span>是否同意</span>
          </label>
          <p class="iv-color-red">《ivva星球用户协议》</p>
        </view>
        <div class="tips">


        </div>
      </div>

    </div>
  </div>
</template>

<script>
  import ajaxUser from "@/utils/service/ajaxUser"
  import Scoket from "@/utils/js/Scoket"
  import tool from "@/utils/js/tool"
  export default {
    props:{
      loginCb:{
        type:Object,
        default(){
          return{}
        }
      }
    },
    data() {
      return {
        checked: true,
        visible: false
      }
    },
    mounted(){
      this._isLogin();
    },
    methods: {
      _isLogin() {
        //判断是否仍在登录状态
        let that = this;
        console.log('_isLogin_isLogin_isLogin')
        wx.checkSession({
          success() {
            //获取用户微信信息
            that._wxGetUserInfo();
            //读取缓存id
            wx.getStorage({
              key: "userId",
              success(res) {
                let url = "";
                const userType = wx.getStorageSync('userType');
                if(userType==-1){
                  console.log(that.loginCb.url,'that.loginCb.url')
                  if(that.loginCb.url){
                    url = `/pages/chatModule/chatList/main?bkurl=${that.loginCb.url}&scene=${that.loginCb.param.scene}`
                  }else {
                    url = "/pages/chatModule/chatList/main"
                  }

                }else if(userType==0){
                  url="/pages/_staffModule/index/main"
                }else {
                  url = "/pages/chatModule/chatList/main"
                }
                wx.redirectTo({url});

                getApp().scoket = new Scoket(getApp());
                that.$store.dispatch('getUserInfo')
              },
              //没有userId，则先判断有无rd_session
              fail() {
                const session = wx.getStorageSync('rd_session');
                if (!session) {
                  that._wxLogin();
                }
                that.visible = true
              }
            });

            // that.getUserInfo()
          },
          fail() {
            console.log('登录失效');
            that._wxLogin();
            that.visible = true
          },
        })
      },
      _getPhoneNumber(e) {
        let that = this;
        let reqdata = {
          encryptedData: e.mp.detail.encryptedData,
          iv: e.mp.detail.iv
        };

        ajaxUser.wxLogin(reqdata, res => {
          console.log("login",res)
          if (res.success) {
            wx.setStorage({
              key: "userId",
              data: res.data.userId
            });
            wx.setStorage({
              key: "userType",
              data: res.data.userType
            });
            wx.setStorage({
              key: "compId",
              data: res.data.compId
            });
            that.visible = false;
            that.$store.commit('setAloneData',{field:"userId",value:res.data.userId});
            that.$store.commit('setAloneData',{field:"userType",value:res.data.userType});
            that.$store.commit('setAloneData',{field:"compId",value:res.data.compId});
            let url = "";
            if(res.data.userType==-1){
              console.log(res.data.userType==-1,'res.data.userType==-1')
              if(that.loginCb.url){
                url = `/pages/chatModule/chatList/main?bkurl=${that.loginCb.url}&scene=${that.loginCb.param.scene}`
              }else {
                url = "/pages/chatModule/chatList/main"
              }
            }else if(res.data.userType==0){
              url="/pages/_staffModule/index/main"
            }else {
              url = "/pages/chatModule/chatList/main"
            }
            wx.redirectTo({url});
            that._wxGetUserInfo();
            getApp().scoket = new Scoket(getApp());
            that.$store.dispatch('getUserInfo')
            // that.getUserInfo()
          }
        });

      },
      _wxLogin(){
        wx.login({
          success: res => {
            let reqdata = {
              js_code: res.code,
            };
            ajaxUser.getSessionKey(reqdata, res => {
              if (res.success) {
                wx.setStorage({
                  key: "rd_session",
                  data: res.data
                });
              }
            })
          }
        });
      },
      _wxGetUserInfo(){
        let that = this;
        wx.getUserInfo({
          success(res){
            res.userInfo.logoUrl = res.userInfo.avatarUrl
            let comfig={
              data:res.userInfo,
            };
            that.$store.commit('setUserInfo',comfig);
            that.globalData.accountInfo = res.userInfo;
            let requestData = {
              unionData: res.encryptedData,
              unionIv: res.iv,
            };
            that.getUnionId(requestData)
          }
        })
      },
      getUnionId(reqdata){
        ajaxUser.getUnionId(reqdata,res=>{
          if(res.success){
            let union=[{field:'unionData',data:reqdata.unionData},{field: 'unionIv',data:reqdata.unionIv},{field: 'unionId',data:res.data}];
            this.$store.commit('setUnion',union);
          }
        })
      },
      //获取用户信息
      getUserInfo(){
        ajaxUser.getUserInfo({},res=>{
          if(res.success){
            this.$store.commit('setUserInfo2',res.data||{})
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  #login-modal {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .3);
    width: 100%;
    height: 100%;
    z-index: 99;
    .modal-wrap {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      .login-wrap {
        width: 80%;
        border-radius: 10px;
        background: #fff;
        padding: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .login-close {
          align-self: flex-end;
          padding: 5px;
        }
        .login-title {
          display: flex;
          justify-content: center;
          font-size: 18px;
          margin: 10px 0 20px;
        }
        .login-btn {
          display: flex;
          align-items: center;
          justify-content: center;
          height: 50px;
          font-size: 16px;
          width: 90%;
          margin-bottom: 15px;
          .iv-icon-wx {
            width: 25px;
            height: 20px;
          }
          .iv-icon-tel {
            width: 18px;
            height: 25px;
          }
        }
        .tips {
          font-size: 12px;
          display: flex;
          align-items: center;
          margin-bottom: 10px;
          .check-label {
            display: flex;
            align-items: center;
          }
          .checkbox {
            zoom: 50%;
          }
        }
      }
    }
  }
</style>
